﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;

namespace FShare.Helpers
{
    public sealed class Utils
    {
        private static readonly Random random = new Random();
        private static readonly StringBuilder dummyData = new StringBuilder(1024);
        private static readonly MD5 md5 = new MD5CryptoServiceProvider();

        public static string GenerateSecret()
        {
            dummyData.Clear();
            int range = random.Next(50, 500);
            for (int i = 0; i < range; i++)
            {
                dummyData.Append(random.Next());
                dummyData.Append(Path.GetRandomFileName());
            }

            var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(dummyData.ToString()));

            dummyData.Clear();
            foreach (var _byte in hash)
            {
                dummyData.Append(_byte.ToString("x2"));
            }

            return dummyData.ToString();
        }

        public static string GenerateFolderName()
        {
            var folderName = GenerateSecret();
            return folderName.Substring(5, 25);
        }

        public static string GenerateServerUrl(string rightPart)
        {
            var uri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + rightPart);
            return uri.ToString();
        }
    }
}